package com.example.hms.service.servicelmpl;

import com.example.hms.entity.*;
import com.example.hms.dao.ReserveDao;
import com.example.hms.service.ReserveRecordsService;
import com.example.hms.service.ReserveService;
import com.example.hms.service.StayService;
import com.example.hms.service.SystemLogService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class ReserveServiceImpl implements ReserveService {
    @Resource
    private ReserveDao reserveDao;

    @Resource
    ReserveRecordsService reserveRecordsService;

    @Resource
    StayService stayService;

    @Resource
    SystemLogService systemLogService;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Result selectAllReserve(Reserve reserve) {
        return ResultSubmit.success(reserveDao.selectAllReserve(reserve));
    }

    @Override
    public Result insert(Reserve reserve, HttpServletRequest request) {
        reserve.setDeleteIdentity("未删除");
        reserve.setReserveTime(format.format(new Date()));
        reserve.setReserveId(Math.abs((int) System.currentTimeMillis()));
        reserve.setAbrogateIdentity("未作废");
        reserve.setLiveIdentity("未收款");
        reserve.setDataUpdateTime(format.format(new Date()));
        ReserveRecords reserveRecords = new ReserveRecords();
        reserveRecords.setReserveId(reserve.getReserveId());
        reserveRecords.setReserveRecordsId(Math.abs((int) System.currentTimeMillis()));
        reserveRecords.setCustomerId(reserve.getCustomerId());
        reserveRecords.setRoomId(reserve.getRoomId());
        reserveRecords.setDataUpdateTime(format.format(new Date()));
        reserveRecords.setDeleteIdentity("未删除");
        // todo 系统记录
        int k = reserveDao.insert(reserve);
        Reserve reserve1 = reserveDao.selectAllReserve(reserve).get(0);
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("reserve");
        systemLog.setUseTableDataIdAfter(String.valueOf(reserve1.getReserveTableId()));
        systemLog.setUseType("订房新增");
        // 调用系统日志服务的插入方法，记录本次操作
        systemLogService.insert(systemLog);
        if (k == 1 && reserveRecordsService.insert(reserveRecords) == 1) {
            return ResultSubmit.success("添加成功");
        } else {
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("添加失败");
        }
    }

    @Override
    public Result update(Reserve reserve, HttpServletRequest request) {
        reserve = reserveDao.selectAllReserve(reserve).get(0);
        Live live = new Live();
        live.setCustomerId(reserve.getCustomerId());
        live.setRoomId(reserve.getRoomId());
        Result result = stayService.insertStay(live,request);
        reserve.setLiveId(Integer.valueOf(result.getData().toString()));

        int i = reserveDao.update(reserve);
        int j = reserveDao.insert(reserve);

        // todo 系统记录
        // 创建系统日志对象，用于记录用户操作
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("reserve");
        systemLog.setUseTableDataIdAfter(String.valueOf(reserve.getReserveTableId()));
        systemLog.setUseTableDataIdBefore(String.valueOf(reserve.getReserveTableId()));
        systemLog.setUseType("订房入住");
        systemLogService.insert(systemLog);

        if (result.getCode() == 200 && i == 1 && j == 1) {
            return ResultSubmit.success("修改成功");
        } else {
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("修改失败");
        }
    }

    @Override
    public Result cancelReserve(Reserve reserve, HttpServletRequest request) {
        reserve = reserveDao.selectAllReserve(reserve).get(0);
        int i = reserveDao.update(reserve);
//        reserve.setDeleteIdentity("已删除");
//        int j = reserveDao.insert(reserve);

        // todo 系统记录
        // 创建系统日志对象，用于记录用户操作
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("reserve");
        systemLog.setUseTableDataIdAfter(String.valueOf(reserve.getReserveTableId()));
        systemLog.setUseTableDataIdBefore(String.valueOf(reserve.getReserveTableId()));
        systemLog.setUseType("订房取消");
        systemLogService.insert(systemLog);
        if (i == 1 ) {
            return ResultSubmit.success("取消成功");
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("取消失败");
        }
    }

    @Override
    public Result abrogateReserve(Reserve reserve, HttpServletRequest request) {
        reserve = reserveDao.selectAllReserve(reserve).get(0);
        int i = reserveDao.update(reserve);
        reserve.setAbrogateIdentity("已作废");
        int j = reserveDao.insert(reserve);
        // todo 系统记录
        // 创建系统日志对象，用于记录用户操作
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("reserve");
        systemLog.setUseTableDataIdAfter(String.valueOf(reserve.getReserveTableId()));
        systemLog.setUseTableDataIdBefore(String.valueOf(reserve.getReserveTableId()));
        systemLog.setUseType("订房作废");
        systemLogService.insert(systemLog);
        if(i == 1 && j == 1){
            return ResultSubmit.success("作废成功");
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("作废失败");
        }
    }
}
